From 70de39e7ecb2ddc01b09c912dabe82a0866eefb1 Mon Sep 17 00:00:00 2001 From: robertlipe Date: Sun, 8 Jun 2014 04:22:44 +0000 Subject: [PATCH] Add a Fatal() and Warning() that take stream operators so we can replace those billion hardcoded calls to sprintf-like substances that use CSTR. --- gpsbabel/GPSBabel.pro | 1 + gpsbabel/logging.h | 70 +++++++++++++++++++++++++++++++++++++++++++ gpsbabel/waypt.cc | 12 ++++---- 3 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 gpsbabel/logging.h diff --git a/gpsbabel/GPSBabel.pro b/gpsbabel/GPSBabel.pro index 5e5c4d508..a78578151 100644 --- a/gpsbabel/GPSBabel.pro +++ b/gpsbabel/GPSBabel.pro @@ -215,6 +215,7 @@ HEADERS = \ jeeps/gpsusbcommon.h \ jeeps/gpsusbint.h \ jeeps/gpsutil.h \ + logging.h \ magellan.h \ mapsend.h \ navilink.h \ diff --git a/gpsbabel/logging.h b/gpsbabel/logging.h new file mode 100644 index 000000000..9226a1ca9 --- /dev/null +++ b/gpsbabel/logging.h @@ -0,0 +1,70 @@ +/* + Copyright (C) 2014 Robert Lipe, robertlipe+source@gpsbabel.org + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA + + */ +#ifndef gpsbabel_logging_h_included +#define gpsbabel_logging_h_included + +// A wrapper for QTextStream that provides a sensible Warning() and Fatal() +// with convenient stream operators. + +#include +#include + +class Warning { + public: + Warning(bool fatal = false) : + fatal_(fatal) { + file_.open(stderr, QIODevice::WriteOnly); + fileStream_.setDevice(&file_); + } + ~Warning() { + fileStream_ << '\n'; + if (fatal_) { + fileStream_.flush(); + exit(1); + } + } + inline Warning& operator << (char d) { fileStream_ << d; return optionalSpace(); } + inline Warning& operator << (signed short d) { fileStream_ << d; return optionalSpace(); } + inline Warning& operator << (unsigned short d) { fileStream_ << d; return optionalSpace(); } + inline Warning& operator << (signed int d) { fileStream_ << d; return optionalSpace(); } + inline Warning& operator << (unsigned int d) { fileStream_ << d; return optionalSpace(); } + inline Warning& operator << (signed long d) { fileStream_ << d; return optionalSpace(); } + inline Warning& operator << (unsigned long d) { fileStream_ << d; return optionalSpace(); } + inline Warning& operator << (float d) { fileStream_ << d; return optionalSpace(); } + inline Warning& operator << (double d) { fileStream_ << d; return optionalSpace(); } + inline Warning& operator << (const char* d) { fileStream_ << QString::fromUtf8(d); return optionalSpace(); } + inline Warning& operator << (QString d) { fileStream_ << '\"' << d << '\"'; return optionalSpace(); } + inline Warning& operator << (const void* d) { fileStream_ << '\"' << d << '\"'; return optionalSpace(); } + + inline Warning& optionalSpace() { + fileStream_ << ' '; + return *this; + } +private: + QFile file_; + QTextStream fileStream_; + bool fatal_; +}; + +class Fatal : public Warning { + public: + Fatal() : Warning(true) {} +}; + +#endif // gpsbabel_logging_h_included diff --git a/gpsbabel/waypt.cc b/gpsbabel/waypt.cc index c2c022e34..44b860831 100644 --- a/gpsbabel/waypt.cc +++ b/gpsbabel/waypt.cc @@ -30,6 +30,8 @@ #include "garmin_fs.h" #include "session.h" +#include + #if NEWQ QList waypt_list; queue waypt_head; // This is here solely to freak out the formats that are @@ -100,12 +102,12 @@ waypt_add(Waypoint* wpt) } if ((wpt->latitude < -90) || (wpt->latitude > 90.0)) - fatal("%s: Invalid latitude %f in waypoint '%s'.\n", - wpt->session->name, - lat_orig, CSTRc(wpt->shortname)); + Fatal() << wpt->session->name + << "Invalid latitude" << lat_orig << "in waypoint" + << wpt->shortname; if ((wpt->longitude < -180) || (wpt->longitude > 180.0)) - fatal("Invalid longitude %f in waypoint '%s'.\n", - lon_orig, CSTRc(wpt->shortname)); + Fatal() << "Invalid longitude" << lon_orig << "in waypoint" + << wpt->shortname; /* * Some input may not have one or more of these types so we -- 2.30.2